<template>
  <div class="songs-container">
    <div class="tab-bar">
      <span class="item" :class="{active:type==0}" @click="type=0">全部</span>
      <span class="item" :class="{active:type==7}" @click="type=7">华语</span>
      <span class="item" :class="{active:type==96}" @click="type=96">欧美</span>
      <span class="item" :class="{active:type==8}" @click="type=8">日本</span>
      <span class="item" :class="{active:type==16}" @click="type=16">韩国</span>
    </div>
    <!-- 底部的table -->
    <table class="el-table playlit-table">
      <thead>
        <th></th>
        <th></th>
        <th>音乐标题</th>
        <th>歌手</th>
        <th>专辑</th>
        <th>时长</th>
      </thead>
      <tbody>
        <tr class="el-table__row" v-for="(item,index) in songPage" :key="index">
          <td>{{index+1}}</td>
          <td>
            <div class="img-wrap">
              <img :src="item.album.picUrl" alt />
              <span class="iconfont icon-play" @click="play(item.id)"></span>
            </div>
          </td>
          <td>
            <div class="song-wrap">
              <div class="name-wrap">
                <span>{{item.name}}</span>
                <span class="iconfont icon-mv" v-if="item.mvid != 0" @click="toMv(item.mvid)"></span>
              </div>
              <span>{{item.alias[0]}}</span>
            </div>
          </td>
          <td>{{item.album.artists[0].name}}</td>
          <td>{{item.album.name}}</td>
          <td>{{item.duration}}</td>
        </tr>
      </tbody>
    </table>

    <!-- 分页器 -->
    <el-pagination
      @current-change="handleCurrentChange"
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
    ></el-pagination>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      type: 0,
      songs: [],
      songPage: [],
      // 总条数
      total: 100,
      // 页码
      page: 1,
      pageSize: 10,
    };
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      this.page = val;
    },
    toMv(id) {
      this.$router.push(`/mv?id=${id}`);
    },
    loadPage() {
      let index = 0;
      let page = [];
      for (let i = (this.page-1)*this.pageSize; i < this.page*this.pageSize; i++) {
        page[index++] = this.songs[i];
      }
      this.songPage = page;
    },
    loadSons() {
      axios({
        url: "/autumnfish/top/song",
        method: "get",
        params: {
          type: this.type,
        },
      }).then((res) => {
        this.songs = res.data.data;
        // 处理时长
        for (let index = 0; index < this.songs.length; index++) {
          let duration = this.songs[index].duration;

          let minute = parseInt(duration / 1000 / 60);
          if (minute < 10) {
            minute = "0" + minute;
          }

          let second = parseInt((duration / 1000) % 60);
          if (second < 10) {
            second = "0" + second;
          }

          this.songs[index].duration = `${minute}:${second}`;
        }
        this.total = this.songs.length;
        this.loadPage();
      });
    },

    play(id) {
      axios({
        url: "autumnfish/song/url",
        method: "get",
        params: {
          id,
        },
      }).then((res) => {
        let url = res.data.data[0].url;
        if (!url) {
          this.$message.error("无法播放这首歌，可能因为没有版权");
        }
        this.$parent.musicUrl = url;
        this.$parent.volume = 0.2;
      });
    },
  },
  watch: {
    type() {
      this.page = 1;
      this.loadSons();
      this.loadPage();
    },
    page() {
      this.loadPage();
    }
  },
  created() {
    this.loadSons();
  },
};
</script>

<style >
</style>
